package com.crm.dao.impl;

import com.crm.dao.EquipmentTypeDao;
import com.crm.model.EquipmentType;
import com.crm.model.PageBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


@Repository("equipmentTypeDao")
public class EquipmentTypeImpl implements EquipmentTypeDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<EquipmentType> list(PageBean pageBean, EquipmentType s_equipmentType) {
        StringBuilder sb = new StringBuilder("select * from t_equipmentType");
        if (s_equipmentType != null) {
            if (StringUtils.isNotEmpty(s_equipmentType.getTypeName())) {
                sb.append(" and typeName like '%").append(s_equipmentType.getTypeName()).append("%'");
            }
        }
        if (pageBean != null) {
            sb.append(" limit ").append(pageBean.getStart()).append(",").append(pageBean.getPageSize());
        }
        final List<EquipmentType> equipmentTypeList = new ArrayList<EquipmentType>();
        jdbcTemplate.query(sb.toString().replaceFirst("and", "where"), new Object[]{}, new RowCallbackHandler() {

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                EquipmentType equipmentType = new EquipmentType();
                equipmentType.setId(rs.getInt("id"));
                equipmentType.setTypeName(rs.getString("typeName"));
                equipmentType.setRemark(rs.getString("remark"));
                equipmentTypeList.add(equipmentType);
            }
        });
        return equipmentTypeList;
    }

    @Override
    public int count(EquipmentType s_equipmentType) {
        StringBuilder sb = new StringBuilder("select count(*) from t_equipmentType");
        if (s_equipmentType != null) {
            if (StringUtils.isNotEmpty(s_equipmentType.getTypeName())) {
                sb.append(" and typeName like '%").append(s_equipmentType.getTypeName()).append("%'");
            }
        }
        return jdbcTemplate.queryForObject(sb.toString().replaceFirst("and", "where"), Integer.class);
    }

    @Override
    public void add(EquipmentType equipmentType) {
        String sql = "insert into t_equipmentType values(null,?,?)";
        jdbcTemplate.update(sql, equipmentType.getTypeName(), equipmentType.getRemark());
    }

    @Override
    public void update(EquipmentType equipmentType) {
        String sql = "update t_equipmentType set typeName=?,remark=? where id=?";
        jdbcTemplate.update(sql, equipmentType.getTypeName(), equipmentType.getRemark(), equipmentType.getId());
    }

    @Override
    public void delete(int id) {
        String sql = "delete from t_equipmentType where id=?";
        jdbcTemplate.update(sql, id);
    }

    @Override
    public EquipmentType loadById(int id) {
        String sql = "select * from t_equipmentType where id=?";
        final EquipmentType equipmentType = new EquipmentType();
        jdbcTemplate.query(sql, new Object[]{id}, new RowCallbackHandler() {

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                equipmentType.setId(rs.getInt("id"));
                equipmentType.setTypeName(rs.getString("typeName"));
                equipmentType.setRemark(rs.getString("remark"));
            }
        });
        return equipmentType;
    }

}
